﻿

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WT.Sockert.Interfaces;

namespace WT.Sockert {
    public class SocketServer : ISocketServer {


        private readonly IPAddress _locationIP;
        public int Port { get; set; }
        public string Location { get; private set; }
        public ISocket ListenerSocket { get; set; }
        private Action<ISocketConnection> _con;

        public SocketServer(string localhost) :
            this(6000, localhost) {

        }
        public SocketServer(int port, string localhost) {

            Port = port;
            
            Location = localhost;
            _locationIP = IPAddress.Parse(localhost);
            var socket = new Socket(_locationIP.AddressFamily, SocketType.Stream, ProtocolType.IP);
            ListenerSocket = new SocketWrapper(socket);
        }


        public void Start(Action<ISocketConnection> con) {

            var ipLocal = new IPEndPoint(_locationIP, Port);
            ListenerSocket.Bind(ipLocal);
            ListenerSocket.Listen(100);
            _con = con;
            ListenForClients();
        }
        /// <summary>
        /// 用户链接
        /// </summary>
        private void ListenForClients() {
            ListenerSocket.Accept(OnClientConnect, e => {
                Console.WriteLine(e.Message);
            });
        }
        private void OnClientConnect(ISocket clientSocket) {
            if (clientSocket == null) return; 
            ListenForClients();
            SocketConnection connection  = new SocketConnection(clientSocket,_con);
            connection.StartReceiving();

            //connection = new WebSocketConnection(
            //    clientSocket,
            //    _config,
            //    bytes => RequestParser.Parse(bytes, _scheme),
            //    r => HandlerFactory.BuildHandler(r,
            //                                     s => connection.OnMessage(s),
            //                                     connection.Close,
            //                                     b => connection.OnBinary(b),
            //                                     b => connection.OnPing(b),
            //                                     b => connection.OnPong(b)),
            //    s => SubProtocolNegotiator.Negotiate(SupportedSubProtocols, s));
            //if (connection.ConnectionInfo != null) {
            //    if (IsSecure) {
            //        FleckLog.Debug("Authenticating Secure Connection");
            //        clientSocket
            //            .Authenticate(Certificate,
            //                          EnabledSslProtocols,
            //                          connection.StartReceiving,
            //                          e => FleckLog.Warn("Failed to Authenticate", e));
            //    } else {
            //        connection.StartReceiving();
            //    }
            //} else {
            //    var socket = new SocketConnection(clientSocket, _config);
            //    socket.StartReceiving();
            //}

        }
    }
}
